home *** CD-ROM | disk | FTP | other *** search
/ PC Media 7 / PC MEDIA CD07.iso / share / prog / cm / cmtcont.cc < prev    next >
Encoding:
Text File  |  1994-09-06  |  1.7 KB  |  70 lines

  1. // CmTCont.cc
  2. // -----------------------------------------------------------------
  3. // Compendium - C++ Container Class Library
  4. // Copyright (C) 1992-1994, Glenn M. Poorman, All rights reserved
  5. // -----------------------------------------------------------------
  6. // Abstract container template implementation.
  7. // -----------------------------------------------------------------
  8.  
  9.  
  10. // "CmTContainer" is the container copy constructor.
  11. //
  12. template <class T> CmTContainer<T>::CmTContainer(const CmTContainer<T>&)
  13.                                   : _error(* new T)
  14. {}
  15.  
  16.  
  17. // "~CmTContainer" is the container destructor.
  18. //
  19. template <class T> CmTContainer<T>::~CmTContainer()
  20. {
  21.   delete &_error;
  22. }
  23.  
  24.  
  25. // "[]" indexing operator returns the item at the specified index.
  26. //
  27. template <class T> const T& CmTContainer<T>::operator[](int idx) const
  28. {
  29.   if (idx < 0 || idx >= size()) return _error;
  30.   CmTIterator<T> *iterator = newIterator();
  31.   int             ii       = 0;
  32.   const T*        out;
  33.  
  34.   while (!iterator->done() && ii <= idx)
  35.   {
  36.     if (ii++ == idx) out = & iterator->current();
  37.     iterator->next();
  38.   }
  39.   delete iterator;
  40.   return *out;
  41. }
  42.  
  43.  
  44. // "size" returns the current container size.
  45. //
  46. template <class T> int CmTContainer<T>::size() const
  47. {
  48.   return _size;
  49. }
  50.  
  51.  
  52. // "isEmpty" returns TRUE if the container is empty.
  53. //
  54. template <class T> Bool CmTContainer<T>::isEmpty() const
  55. {
  56.   return (_size == 0);
  57. }
  58.  
  59.  
  60. // "copy" copies the contents of the specified container into
  61. // this container.
  62. //
  63. template <class T> void CmTContainer<T>::copy(const CmTContainer<T>& C)
  64. {
  65.   _size = C._size;
  66.   CmTIterator<T> *iterator = C.newIterator();
  67.   while (!iterator->done()) add(iterator->next());
  68.   delete iterator;
  69. }
  70.